Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nativescript-webworkers

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nativescript-webworkers

A WebWorker (thread) NativeScript module for Android and iOS

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

npm npm npm

nativescript-webworkers

NativeScript WebWorkers (threads) for Android and iOS This is really only useful if you need to access a hidden WebView now; NativeScript has internal support for WebWorkers for a long time now.

License

This is released under the MIT License, meaning you are free to include this in any type of program -- However for entities that need a support contract, changes, enhancements and/or a commercial license please contact me at http://nativescript.tools.

I also do contract work; so if you have a module you want built for NativeScript (or any other software projects) feel free to contact me nathan@master-technology.com.

Donate Patreon

Breaking Changes

v2.00 No longer attempts to assign the global Worker object; NativeScript has Native WebWorker support for a long time now.

Notes

Please note The WebWorker environment is a LIMITED JavaScript shell. It does have AJAX/HTTP(s) ability; but it does NOT have any access to any of the NativeScript api. It also does not have any access to the Native iOS or Android api's. It is strictly a JS thread. In addition at this moment "ImportScripts" has not been implemented. It should be fairly easy to implement; and if anyone wants to do a pull request to implement this; I'd be happy to add it to the code that gets injected into the environment.

If the device has more than one CPU; the Worker threads should not impact the main NativeScript thread. However, if the device only has one CPU; and depending on how busy you make the worker, it will impact the primary NativeScript thread.

Installation

Run tns plugin add nativescript-webworkers in your ROOT directory of your project.

Usage

To use the module you must first require() it:

const WebWorker = require( 'nativescript-webworkers' );

const myWorker = new WebWorker('~/web.worker.js'); // i.e. using the WebWorker variable returned by the require statement.

new WebWorker(data)

data can be:

  • File path to a JS file (either relative ~/.../somescript.js or full path /..../somescript.js)
  • Object with a .HTML - which allows you to pass in your own HTML file
    • please make sure the HTML is proper and not malformed.
  • Object with a .script - which allows you to pass in your own javascript code
    • Object can have its own custom .baseURL, otherwise defaults to the proper baseURL.

NativeScript environment

Events

####.onmessage(data) This will have the JSON object that was sent from the worker

####.onerror This will have any errors that occurred (this may be unreliable; as not all errors can be tracked properly)

####.onready This is fired when the webworker environment is ready to go.
This is NOT a standard webworker function

.executeJS

This allows you to send some JS into the environment. This is NOT a standard webworker function

Methods

####.postMessage(data) This posts the message into the webworker environment ####.terminate() This terminates the webworker environment; after this is ran; DO NOT attempt to continue doing anything on this webworker.

WebWorker Environment

Events

####onmessage(data) The function that gets any messages from the NativeScript environment

####onready() This function will get called if it exists once the webworker is able to communicate with the NativeScript host. This is NOT a standard webworker function

Methods

####postMessage(data) The function you use to send message back to the NativeScript environment

####close() Terminates this environment

##Example:

NativeScript code:

  const WebWorker = require( 'nativescript-webworkers' );
  var myWorker = new WebWorker('~/test.worker.js');
  myWorker.onmessage = function(m) { console.log("Webworker said:", m); };
  myWorker.postMessage("Hello");

test.worker.js

  onmessage = function(m) {
    console.log("NativeScript said" + m);	
  };
  onready = function() {
    postMessage("Hi");
  };

Keywords

FAQs

Package last updated on 23 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc